package com.tanhua.server.controller;

import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    /**
     * 发布动态
     * @param imageContent
     * @param paramMap
     * @return
     */
    @PostMapping
    public ResponseEntity post(MultipartFile[] imageContent,@RequestParam Map<String,String> paramMap){
        movementService.post(imageContent, paramMap);
        return ResponseEntity.ok(null);
    }

    /**
     * 查看个人动态
     * @param page
     * @param pagesize
     * @param userId
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity all(Long page, Long pagesize, Long userId){
        PageResult<MovementsVo> pageResult = movementService.findPageByUserId(page, pagesize, userId);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 朋友圈，查询朋友们发布的动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity movements(Long page, Long pagesize){
        PageResult<MovementsVo> pageResult = movementService.findFriendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 推荐动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(Long page, Long pagesize){
        PageResult<MovementsVo> pageResult = movementService.recommend(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询单条动态
     * @param movementId
     * @return
     */
    @GetMapping("/{movementId}")
    public ResponseEntity findById(@PathVariable String movementId){
        MovementsVo vo = movementService.findById(movementId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 首页中的 最近访客
     * 与单条动态api冲突,先让它通过，后续处理
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity myVisitors(){
        List<VisitorsVo> voList = movementService.findLast4Visitors();
        return ResponseEntity.ok(voList);
    }

    /**
     * 点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{movementId}/like")
    public ResponseEntity like(@PathVariable String movementId){
        Integer likeCount = movementService.like(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{movementId}/dislike")
    public ResponseEntity dislike(@PathVariable String movementId){
        Integer likeCount = movementService.dislike(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 喜欢
     * @param movementId
     * @return
     */
    @GetMapping("/{movementId}/love")
    public ResponseEntity love(@PathVariable String movementId){
        Integer likeCount = movementService.love(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消喜欢
     * @param movementId
     * @return
     */
    @GetMapping("/{movementId}/unlove")
    public ResponseEntity unlove(@PathVariable String movementId){
        Integer likeCount = movementService.unlove(movementId);
        return ResponseEntity.ok(likeCount);
    }
}
